//千禾关注
//长距离测试
//sml_move(400, 1800, 800, 230, 1000);
//短距离测试
//sml_move(400, 1000, 400, 300, 1000);
function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = {
        "x": 0,
        "y": 0
    }
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
    var xxy = [time];
    var point = [];
    var dx0 = {
        "x": qx,
        "y": qy
    }

    var dx1 = {
        "x": random(qx - 100, qx + 100),
        "y": random(qy, qy + 50)
    }
    var dx2 = {
        "x": random(zx - 100, zx + 100),
        "y": random(zy, zy + 50),
    }
    var dx3 = {
        "x": zx,
        "y": zy
    }
    for (var i = 0; i < 4; i++) {

        eval("point.push(dx" + i + ")");

    }
    //toastLog(point[3].x)

    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]

        xxy.push(xxyy);

    }

    //toastLog(xxy);
    gesture.apply(null, xxy);
}


//sml_move(400, 1800, 800, 230, 2000);

//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
    switch (arguments.length) {
        case 1:
            return parseInt(Math.random() * minNum + 1, 10);
            break;
        case 2:
            return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            //或者 Math.floor(Math.random()*( maxNum - minNum + 1 ) + minNum );
            break;
        default:
            return 0;
            break;
    }
}

function up() {
    let rNum = randomNum(100, 400);
    toastLog("up");
    toastLog("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down() {
    let rNum = randomNum(100, 400);
    let yNum = randomNum(950, 1000);
    toastLog("down");
    toastLog("x:" + rNum);
    toastLog("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function up1(rNum) {
    toastLog("up");
    toastLog("x:" + rNum);
    sml_move(rNum, 1000, rNum, rNum, 1000);
}

function down1(rNum, yNum) {
    toastLog("down");
    toastLog("x:" + rNum);
    toastLog("y:" + yNum);
    sml_move(rNum, 300, rNum, yNum, 1000);
}

function closeApp(packagename) {
    var sh = new Shell(true);
    //注册一个对象运用shell命令，true(真) 以root权限运行代码，默认为falae假
    // console.toastLog("am force-stop"+" "+packagename)
    sh.exec("am force-stop" + " " + packagename); //执行代码运行中属于异步运行
    //上面值com.android.browser是浏览器的包名。自行修改成想停止软件的包名
    sleep(1000);
    //给点延迟让前面的运行命令，一会软件就会关闭
    sh.exit;
    //退出Shell命令，正在执行的命令会被强制退出。所以上面加延迟
    toast("关闭" + packagename);
}

function closeApk(packageName) {
    var name = getPackageName(packageName);
    if (!name) {
        if (getAppName(packageName)) {
            name = packageName;
        } else {
            return false;
        }
    }
    app.openAppSetting(name);
    text(app.getAppName(name)).waitFor();
    let is_sure = textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne();
    if (is_sure.enabled()) {
        textMatches(/(.*强.*|.*停.*|.*结.*|.*行.*)/).findOne().click();
        textMatches(/(.*确.*|.*定.*)/).findOne().click();
        toastLog(app.getAppName(name) + "应用已被关闭");
        sleep(1000);
        back();
    } else {
        toastLog(app.getAppName(name) + "应用不能被正常关闭或不在后台运行");
        back();
    }
}


var p1 = 1000;
var p2 = 2000;
var p3 = 3000;
var p4 = 4000;
var p5 = 5000;

auto();
auto.waitFor();

// console.setPosition(150, 370);
// console.show();

let msg = "https://www.qliang2.com";

let packageName = app.getPackageName("微信");
toastLog(packageName);

function startApp(){
    closeApk(packageName);

    app.launchPackage(packageName);
    sleep(p5);

    textContains("微信").waitFor();
    sleep(p1);

    while(true){
        sleep(1000);
        let ca = currentActivity();
        toastLog(ca);
        if(ca == 'com.tencent.mm.ui.LauncherUI'){
            break;
        }
    }
    
}

startApp();

function goToWebBrowse(){
    textContains("微信").waitFor();
    sleep(2000);
    click("我");
    sleep(2000);
    click("收藏");
    sleep(2000);
    click("磊帝森");
    sleep(2000);
    press(353,912,3000);
    sleep(2000);
    click("识别图中的二维码");
    sleep(5000);
    textContains("千禾").waitFor();
    toastLog("千禾");
    sleep(2000);
    textContains("复制链接").waitFor();
    toastLog("复制链接");
    sleep(2000);
    textContains("复制链接").waitFor();
    sleep(1000);
    click(670,88);
    sleep(1000);
    let r = textContains("在浏览器打开").findOne(5000);
    toastLog("在浏览器打开:");
    if(null != r){
        toastLog("有");
        let p = r.parent();
        toastLog("在浏览器打开parent控件:");
        if(null !=p){
            toastLog("有");
            p.click();
            sleep(1000);
        }
    }
    let v = textContains("Via").findOne(5000);
    toastLog("Via:");
    if(null != v){
        toastLog("有");
        click(100,960);
        sleep(1000);
    }
}

goToWebBrowse();

function doFocus(){
    sleep(5000);
    textContains("任务列表").findOne(5000);
    sleep(1000);
    click("关注任务");
    sleep(2000);
    up1(80);
    sleep(2000);
    click("剩余");
    sleep(2000);
    click("跳过指引");
    sleep(2000);
    click("是");
    sleep(2000);
    up1(80);
    sleep(2000);
    click("复制");
    sleep(2000);
    click("保存二维码");
    sleep(2000);
    press(355,550,3000);
    sleep(2000);
    click("保存图片");
    sleep(2000);
    click("确定");
    closeApk("微信");
    sleep(2000);
    app.launchApp("微信");
    sleep(2000);
    textContains("微信").waitFor();
    sleep(2000);
    while(true){
        sleep(1000);
        let fx= click("发现");
        toastLog(fx);
        if(fx){
            toastLog("发现，break");
            break;
        }
    }
    sleep(2000);
    textContains("发现").waitFor();
    sleep(5000);
    click("扫一扫");
    sleep(2000);
    textContains("扫码").findOne(5000);
    sleep(2000);
    click(628,950);
    sleep(2000);
    click(270,222);
    sleep(2000);
    for(let i=1;i<=10;i++){
        sleep(1000);
        let ck= click("关注");
        toastLog(i+":"+ck);
        if(ck){
            toastLog("关注，break");
            break;
        }
    }    
    sleep(5000);
    let m = desc("消息").findOne(5000);
    if(null != m){
        toastLog("切换消息");
        m.click();
        sleep(1000);
    }
    setText(getClip());
    sleep(3000);
    let fs = click("发送");
    toastLog("发送:",fs);
    sleep(3000);
    back();
    sleep(3000);
    back();
    app.launchApp("Via");
    while(true){
        sleep(100);
        let task = textContains("任务列表").findOne(5000);
        if(null != task){
            toastLog("break");
            break;
        }
        sleep(100);
        let dr = desc("刷新").findOne(5000);
        if(null != dr){
            dr.click();
            sleep(2000);
            let tk1 = textContains("任务列表").findOne(2000);
        }
        let tt= randomNum(1,3);
        toastLog("tt:"+tt);
        sleep(tt*1000);
        back();
        toastLog("back");
    }
}

function begin() {
    var count = 0;
    while (true) {
        toastLog('count:' + count);
        doFocus();
        count++;
    };
};

begin();